home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.netins.net!isac!gg
- From: gg@isac.hces.com (Greg Goodrich)
- Subject: Re: Using char array in functions
- Message-ID: <1996Feb29.230019.5028@isac.hces.com>
- Organization: Health Care Expert Systems
- X-Newsreader: TIN [version 1.2 PL2]
- References: <20FEB199609155775@sundog.caltech.edu> <4gqk5m$jno@aphex.direct.ca>
- Date: Thu, 29 Feb 1996 23:00:19 GMT
-
- Ed Toivanen (etoivane@direct.ca) wrote:
- : In article <20FEB199609155775@sundog.caltech.edu>,
- : taylor@sundog.caltech.edu395-3807,MC264-33CALTECHPASADENACA91125 says...
- : >
- : >I've searched the faq's and postings, but still am looking for a clear
- : >explanation on how to use arrays of char strings in functions, especially when
- : >I want to have the function modify the array. I find I can't just pass the
- : >name of the string as a pointer as I do for single strings.
- : ^^^^
-
- : As I just learned from my midterm(the hard way, in other words!) you have to
- : pass the address of the name of the array if you expext to modify more than a
- : copy of the thing.
-
- Take the following example:
-
- /* prototype for function */
- void foo(char *bar);
-
- /* declaration */
- char array1[100];
-
- main()
- {
- foo(array1);
- }
-
- In this example, the function foo() can modify any member of the array
- "array1" at will. You do not have to pass the address of the array
- name, because by using the array name, you are actually passing
- &array1[0], or in english, the address of the first member of the array,
- which for argument's sake is also the address of the array. This allows
- any manipulation of the array inside the function that the function
- wishes. As a matter of fact, passing a copy of an array into a function
- is the real challenge, and is usually done in a roundabout sort of way
- by placing the array inside a structure and passing the entire structure
- as a parameter to the function. Another method is to declare two
- arrays, and before calling the function, copy the first array into the
- second by using something like memcpy().
-
- Greg.
- --
- _______________________________________
- Greg Goodrich - gg@hces.com
- Software Engineer
- PACE Health Management Systems
-